package org.demo.multisource.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.demo.multisource.entity.Student;
import org.demo.multisource.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;

public interface StudentService extends IService<Student> {

    void cusAdd();

    @Service("studentService")
    class Default extends ServiceImpl<StudentMapper,Student> implements StudentService{

        @Autowired@Lazy
        private StudentService thix;
        @Autowired
        private StudentMapper mapper;

        @Override
        public void cusAdd() {
            Student student = new Student();
            student.setName("cus_add");
            student.setCreatetime(LocalDateTime.now());
            mapper.cusAdd(student);
            // selectOne用的是SqlSessionTemplate
            Student newStudent = mapper.selectOne(new LambdaQueryWrapper<Student>().eq(Student::getId,student.getId()));
            // 批量操作用的是DefaultSqlSessionFactory
            thix.saveOrUpdateBatch(Collections.singletonList(newStudent));
        }
    }
}
